package com.mySampleApplication.client.components;

import com.mySampleApplication.client.components.item.*;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Dialog;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.mySampleApplication.client.components.item.*;

/**
 * 用法
 *      // 创建按钮和事件处理
*      Button addButton = new Button("Add");
*      addButton.addClickHandler(event -> showAddDialog(listGrid));
 *     private static void showAddDialog(ListGrid listGrid) {
 *        Dialog dialog = SDialog.createDialog("Add Item", null,listGrid);
 *         dialog.show();
 *     }
 */
public class SDialog {
    public static Dialog createDialog(int width, int height,String title, final ListGridRecord record, ListGrid listGrid) {
        Dialog dialog = new Dialog();
        dialog.setTitle(title);
        dialog.setWidth(width);
        dialog.setHeight(height);
        dialog.setShowMinimizeButton(false);
        dialog.setIsModal(true);
        dialog.setShowModalMask(true);
        dialog.setCanDragResize(true);
        dialog.centerInPage();

        DynamicForm form = new DynamicForm();
        form.setPadding(10);
        form.setWidth100();
        form.setHeight(height-100);
        form.setNumCols(4);
        form.setCellPadding(10);


        SelectItem selectItem = SSelectItem.getSelectItem(false);
        SelectItem selectItems = SSelectItem.getSelectItem(true);

        SelectItem selectItemListGrid = SSelectItem.getSelectItemListGrid(false);
        SelectItem selectItemsListGrid = SSelectItem.getSelectItemListGrid(true);

        SelectItem radioItem = SRadioItem.getRadioItem();

        CanvasItem checkboxItem = SCheckboxItem.getCheckboxItem();

        CanvasItem treeDragNodes = STreeDragNodesSample.getTreeDragNodes();

        CanvasItem treeDragReparent = STreeDragReparentSample.getTreeDragReparent();

        form.setItems(selectItem,selectItems,selectItemListGrid,selectItemsListGrid,radioItem,treeDragNodes,treeDragReparent);
        //form.setFields(checkboxItem);
        Button saveButton = new Button("Save");
        saveButton.addClickHandler(event -> {
            /*if (form.validate()) {
                if (record != null) {
                    // 更新已有记录
                    record.setAttribute("name", nameItem.getValueAsString());
                    record.setAttribute("age", ageItem.getValueAsString());
                    listGrid.updateData(record);
                } else {
                    // 添加新记录
                    ListGridRecord newRecord = new ListGridRecord();
                    newRecord.setAttribute("name", nameItem.getValueAsString());
                    newRecord.setAttribute("age", ageItem.getValueAsString());
                    listGrid.addData(newRecord);
                }
                dialog.hide();
            }*/
        });

        Button cancelButton = new Button("Cancel");
        cancelButton.addClickHandler(event -> dialog.hide());

        dialog.addItem(form);

        HLayout buttonLayout = new HLayout();
        buttonLayout.setMembersMargin(10);
        buttonLayout.setAlign(Alignment.RIGHT);
        buttonLayout.setWidth100();
        buttonLayout.setHeight(30);
        buttonLayout.setPadding(5);
        buttonLayout.setLayoutAlign(VerticalAlignment.BOTTOM);
        buttonLayout.addMembers(saveButton, cancelButton);
        dialog.addItem(buttonLayout);
        return dialog;
    }
}
